{-# OPTIONS_GHC -XTemplateHaskell #-}
module Text.XML.Light.Xmlable.Utils where

import Language.Haskell.TH.Syntax
import Data.PackedString
import Text.XML.Light.Types

qualName :: Name -> Q Exp
qualName t = do
    TyConI (DataD _ name _ _ _)  <- reify t
    uri <- return $ Just (mkURI $ show name)
    [| \s -> QName { qName = s, qURI = uri, qPrefix = Nothing } |]
    where
        mkURI mn = "http://haskell.org/" ++ change mn
            where 
                change = change' . break (=='.')
                change' (a,b)
                    | null b = a
                    | not ('.' `elem` b) = a
                    | otherwise = a ++ ('/' : change (tail b))

{-                    
-- from Bulat Ziganshin's "Template Haskell Tutorial"
sel :: Int -> Int -> Q Exp
sel n m = do 
    x <- newName "x"
    return (LamE (replacen (replicate m WildP) (VarP x)) (VarE x))
    where
        replacen xs x  =  take (n-1) xs ++ x : drop n xs
-}